//
//  LGAddAnomalyNormController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/2.
//  新增异常指标

#import "LGAddAnomalyNormController.h"
//选择指标
#import "LGSelectNormController.h"
#import "LGAddAnomalyLineView.h"
#import "LGPickerView.h"
#define lineHeigh  viewPix(50)
@interface LGAddAnomalyNormController()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**  指标名称 */
@property (nonatomic , strong)LGAddAnomalyLineView *nameView;
/**  结果数值 */
@property (nonatomic , strong)LGAddAnomalyLineView *valueView;
/**  异常选择 */
@property (nonatomic , strong)LGAddAnomalyLineView *anomalyView;
/**  参考值 */
@property (nonatomic , strong)LGAddAnomalyLineView *referenceView;
/**  单位 */
@property (nonatomic , strong)LGAddAnomalyLineView *unitView;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSArray *arrowArray;
/**   */
@property (nonatomic , copy)NSString *idString;

@end

@implementation LGAddAnomalyNormController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self createSubView];
    self.navigationView.title = @"新增异常指标";
    if(_dataDic && _dataDic.allKeys.count>0){
        self.navigationView.title = @"编辑异常指标";
        [self.saveBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    }else{
        self.idString = @"";
    }
    [self requestArrowList];
}

-(void)requestArrowList{
    [RequestUtil withPOST:@"/doctor/other.inspection_report_type/typeList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSArray *arrowArray = returnData[@"arrows"];
            NSMutableArray *tempArray = [NSMutableArray array];
            [tempArray addObject:@"请选择"];
            for (NSInteger i=0; i<arrowArray.count; i++) {
                NSDictionary *tempDic = arrowArray[i];
                [tempArray addObject:LGNSString(tempDic[@"value"])];
            }
            self.arrowArray = [NSArray arrayWithArray:tempArray];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.idString = LGNSString(dataDic[@"id"]);
    self.nameView.contentTF.text = LGNSString(dataDic[@"entry_name"]);
    self.valueView.contentTF.text = LGNSString(dataDic[@"result"]);
    self.anomalyView.contentTF.text = LGNSString(dataDic[@"arrow"]);
    self.referenceView.contentTF.text = LGNSString(dataDic[@"range"]);
    self.unitView.contentTF.text = LGNSString(dataDic[@"unit"]);
}

//指标名称、异常项
-(void)selectButtonAction:(UIButton *)sender{
    [self.view endEditing:YES];
    if(sender.superview == self.nameView){
        //指标名称
        LGSelectNormController *controller = [[LGSelectNormController alloc]init];
        controller.normid = self.normid;
        [self.navigationController pushViewController:controller animated:YES];
        __weak typeof(self) weakSelf = self;
        controller.selectNormData = ^(NSDictionary * _Nonnull dataDic) {
            weakSelf.nameView.contentTF.text = LGNSString(dataDic[@"title"]);
            weakSelf.referenceView.contentTF.text = LGNSString(dataDic[@"reference_value"]);
            weakSelf.unitView.contentTF.text = LGNSString(dataDic[@"unit"]);
            weakSelf.valueView.contentTF.text = @"";
            weakSelf.anomalyView.contentTF.text = @"";
        };
    }else{
        //异常选择
        if(self.arrowArray.count == 0){
            [self requestArrowList];
        }else{
            LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
            pickerView.pickArry = @[self.arrowArray];
            pickerView.selectStr = self.anomalyView.contentTF.text;
            [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
            __weak typeof(self) weakSelf = self;
            pickerView.selectPickViewResult = ^(NSArray *resultArray) {
                weakSelf.anomalyView.contentTF.text = LGNSString(resultArray[0]);
            };
        }
    }
}

//保存
-(void)saveBtnAction{
    if(self.nameView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请选择指标名称" offset:0];
        return;
    }
    if (self.valueView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请填写结果数值" offset:0];
        return;
    }
    if (self.anomalyView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请选择异常选择" offset:0];
        return;
    }
    if (self.referenceView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请填写参考值" offset:0];
        return;
    }
    if (self.unitView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请填写单位" offset:0];
        return;
    }

    NSString *arrow = [self.anomalyView.contentTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    arrow = [arrow stringByReplacingOccurrencesOfString:@"请选择" withString:@""];
    NSDictionary *tempDic = @{@"id":self.idString,@"entry_name":self.nameView.contentTF.text,@"result":self.valueView.contentTF.text,@"arrow":arrow,@"range":self.referenceView.contentTF.text,@"unit":self.unitView.contentTF.text};
    if(_dataDic && _dataDic.allKeys.count>0){
        //修改
        if(self.editAnomalyNormFinished){
            self.editAnomalyNormFinished(tempDic , self.index);
        }
    }else{
        //添加
        if(self.addAnomalyNormSuccess){
            self.addAnomalyNormSuccess(tempDic);
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.baseView];
    [self.baseView addSubview:self.nameView];
    [self.baseView addSubview:self.valueView];
    [self.baseView addSubview:self.anomalyView];
    [self.baseView addSubview:self.referenceView];
    [self.baseView addSubview:self.unitView];
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.saveBtn];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.frame = CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), lineHeigh*5);
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(LGAddAnomalyLineView *)nameView{
    if(!_nameView){
        _nameView = [[LGAddAnomalyLineView alloc] initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), lineHeigh)];
        _nameView.titleLabel.text = @"指标名称";
        _nameView.contentTF.placeholder = @"请选择指标名称";
        _nameView.contentTF.userInteractionEnabled = NO;
        _nameView.contentTF.rightViewMode = UITextFieldViewModeAlways;
        _nameView.contentTF.rightView = [self rightView];
        [_nameView addSubview:[self actionButton]];
    }
    return _nameView;
}

-(LGAddAnomalyLineView *)valueView{
    if(!_valueView){
        _valueView = [[LGAddAnomalyLineView alloc] initWithFrame:CGRectMake(0, kMaxY(self.nameView.frame), Screen_W-viewPix(32), lineHeigh)];
        _valueView.titleLabel.text = @"结果数值";
        _valueView.contentTF.placeholder = @"请输入数值";
    }
    return _valueView;
}

-(LGAddAnomalyLineView *)anomalyView{
    if(!_anomalyView){
        _anomalyView = [[LGAddAnomalyLineView alloc] initWithFrame:CGRectMake(0, kMaxY(self.valueView.frame), Screen_W-viewPix(32), lineHeigh)];
        _anomalyView.titleLabel.text = @"异常选择";
        _anomalyView.contentTF.placeholder = @"请选择";
        _anomalyView.contentTF.userInteractionEnabled = NO;
        _anomalyView.contentTF.rightViewMode = UITextFieldViewModeAlways;
        _anomalyView.contentTF.rightView = [self rightView];
        [_anomalyView addSubview:[self actionButton]];
    }
    return _anomalyView;
}

-(LGAddAnomalyLineView *)referenceView{
    if(!_referenceView){
        _referenceView = [[LGAddAnomalyLineView alloc] initWithFrame:CGRectMake(0, kMaxY(self.anomalyView.frame), Screen_W-viewPix(32), lineHeigh)];
        _referenceView.titleLabel.text = @"参考值";
        _referenceView.contentTF.placeholder = @"请输入参考值";
    }
    return _referenceView;
}

-(LGAddAnomalyLineView *)unitView{
    if(!_unitView){
        _unitView = [[LGAddAnomalyLineView alloc] initWithFrame:CGRectMake(0, kMaxY(self.referenceView.frame), Screen_W-viewPix(32), lineHeigh)];
        _unitView.titleLabel.text = @"单位";
        _unitView.contentTF.placeholder = @"请输入单位";
        _unitView.lineView.hidden = YES;
    }
    return _unitView;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H-viewPix(84)-bottomSafeBarHeight/4.0, Screen_W, viewPix(84)+bottomSafeBarHeight/4.0)];
    }
    return _bottomView;
}

-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:viewPix(12)];
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

-(UIView *)rightView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(17), lineHeigh)];
    UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    tipView.frame = CGRectMake(viewPix(5), (lineHeigh-viewPix(12))/2.0, viewPix(12), viewPix(12));
    [baseView addSubview:tipView];
    return baseView;
}

-(UIButton *)actionButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, Screen_W-viewPix(32), lineHeigh);
    [button addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

@end
